package com.whlilong.jxc.editor.support;

import java.math.BigDecimal;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;

import com.whlilong.jxc.listener.IEditorListener;
import com.whlilong.jxc.listener.ITextKeyChangeListener;
import com.whlilong.jxc.model.Purcharsbook;

public class PurcharsBookEdtiorSupport extends EditingSupport implements
		ITextKeyChangeListener {

	/**
	 * 当前第几列
	 */
	private int currentColumn;

	private boolean isFirst = true;

	private CellEditor editor;

	public PurcharsBookEdtiorSupport(TableViewer viewer, int column,
			IEditorListener listener) {
		super(viewer);
		this.currentColumn = column;
		switch (column) {
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
			editor = new PurcharsBookTextKeyCellEditor(viewer.getTable(), listener,
					column, this);
			break;
		default:
			editor = null;
		}
	}

	@Override
	protected boolean canEdit(Object element) {
		// TODO Auto-generated method stub
		return (currentColumn != 1 && currentColumn !=2);
	}

	@Override
	protected CellEditor getCellEditor(Object element) {
		return editor;
	}

	@Override
	protected Object getValue(Object element) {
		Purcharsbook obj = (Purcharsbook) element;
		switch (currentColumn) {
		case 3:
			return obj.getBarcode();
		case 4:
			return obj.getGoodsname();
		case 5:
			return obj.getUnit();
		case 6:
			return obj.getCout().toString();
		case 7:
			return obj.getOriginalprice().toString();
		case 8:
			return obj.getDiscount().toString();
		case 9:
			return obj.getPrice().toString();
		case 10:
			return obj.getMoney().toString();
		case 11:
			return obj.getReachcount().toString();
		case 12:
			return obj.getStorageup().toString();
		case 13:
			return obj.getStoragedown().toString();
		case 14:
			return obj.getStoragecount().toString();
		
		}

		return null;
	}

	@Override
	protected void setValue(Object element, Object value) {
		Purcharsbook obj = (Purcharsbook) element;
		switch (currentColumn) {
		case 3:
			obj.setBarcode(String.valueOf(value));
			break;
		case 4:
			obj.setGoodsname(String.valueOf(value));
			break;
		case 5:
			obj.setUnit(String.valueOf(value));
			break;
		case 6:
			obj.setCout(Integer.valueOf(value.toString()));
			break;
		case 7:
			obj.setOriginalprice(BigDecimal.valueOf(Double.valueOf(value.toString())));
			break;
		case 8:
			obj.setDiscount(BigDecimal.valueOf(Double.valueOf(value.toString())));
			break;
		case 9:
			obj.setPrice(BigDecimal.valueOf(Double.valueOf(value.toString())));
			break;
		case 10:
			obj.setMoney(BigDecimal.valueOf(Double.valueOf(value.toString())));
			break;
		case 11:
			obj.setReachcount(Integer.valueOf(value.toString()));
			break;
		case 12:
			obj.setStorageup(Integer.valueOf(value.toString()));
			break;
		case 13:
			obj.setStoragedown(Integer.valueOf(value.toString()));
			break;
		case 14:
			obj.setStoragecount(Integer.valueOf(value.toString()));
			break;

		}
		getViewer().update(element, null);

	}

	@Override
	public void changeEvent() {
		if (editor instanceof GoodsInfoTextKeyCellEditor)
			((GoodsInfoTextKeyCellEditor) editor).releaseKey();

	}

	@Override
	public void modify() {
		isFirst = true;
	}

}
